<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>基础表单</title>
    <link rel="stylesheet" href="__LIBS__/layui/css/layui.css"/>
    <link rel="stylesheet" href="__MODULE__/admin.css?v=317"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-upload-img {
            width: 200px;
            min-height: 120px;
            height: auto;
            margin: 0 10px 10px 0;
        }
        .maincolor-box{
            display: flex;
            align-items: center;
        }
        .maincolor{
            width: 60px;
            height: 40px;
            margin-left: 10px;
            display: inline-block;
        }

    </style>
</head>
<body>
<!-- 加载动画 -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加幻灯片 </div>
        <div class="layui-card-body">
            <form class="layui-form" id="formBasForm" lay-filter="formBasForm" style="max-width: 960px;">
                <input type="hidden" name="id" value="{$item['id']?$item['id']:''}">
                <input type="hidden" name="status" value="{$item['status']?$item['status']:''}">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">名称:</label>
                    <div class="layui-input-block">
                        <input name="title"  class="layui-input" required lay-verify="required" value="{$item['title']?$item['title']:''}"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">图片:</label>
                    <input type="hidden" name="thumb" lay-verify="required" required value="{$item['thumb']?$item['thumb']:''}">
                    <div class="layui-upload layui-input-block">
                        <button type="button" class="layui-btn" id="test1"><i class="layui-icon">&#xe67c;</i>上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" src="{$item['thumb']?$item['thumb']:''}" id="demo1" crossorigin="anonymous">
                            <p id="demoText"></p>
                        </div>
                        <div class="layui-upload-list maincolor-box">
                            <div class="maincolor-label">幻灯片背景色:</div>
                            <div class="maincolor"></div>
                            <input type="hidden" name="maincolor" value="{$item['maincolor']?$item['maincolor']:''}">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">幻灯片归属:</label>
                    <div class="layui-input-inline">
                        {if $item}
                        <select name="shoptype" required lay-verify="required">
                            <option value="">请选择主体类型</option>
                            <option value="zsff" {$item['shoptype']=='zsff'?'selected':''}>知识店铺</option>
                            <option value="mall" {$item['shoptype']=='mall'?'selected':''}>教辅商城</option>
                            <option value="pxjg" {$item['shoptype']=='pxjg'?'selected':''}>培训机构</option>
                            <option value="credit" {$item['shoptype']=='credit'?'selected':''}>积分商城</option>
                        </select>
                        {else /}
                        <select name="shoptype" required lay-verify="required">
                            <option value="">请选择主体类型</option>
                            <option value="zsff" >知识店铺</option>
                            <option value="mall" >教辅商城</option>
                            <option value="pxjg" >培训机构</option>
                            <option value="credit">积分商城</option>
                        </select>
                        {/if}
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">公众号链接:</label>
                    <div class="layui-input-block">
                        <input name="gzh_src"  class="layui-input" value="{$item['gzh_src']?$item['gzh_src']:''}"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">小程序链接:</label>
                    <div class="layui-input-block">
                        <input name="xcx_src"  class="layui-input" value="{$item['xcx_src']?$item['xcx_src']:''}"/>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text" >
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block text-center">
                        <button class="layui-btn" lay-filter="formBasSubmit" lay-submit>&emsp;提交&emsp;</button>
                        <button ew-event="closeThisTabs" type="button" class="layui-btn layui-btn-primary">&emsp;关闭&emsp;</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- js部分 -->
<script type="text/javascript" src="__LIBS__/layui/layui.js"></script>
<script type="text/javascript" src="__NJS__/common.js?v=317"></script>
<script type="text/javascript" src="__LIBS__/colorthief/color-thief.umd.js"></script>
<script>
    layui.use(['layer', 'form', 'laydate','upload','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var admin = layui.admin;
        var item=$("input[name='thumb']").val()
        if(item!=''){
            getcolor()
        }
        //getcolor()
        function getcolor(){
            const colorThief = new ColorThief();
            const img = document.querySelector('img');
            const getColorFun=()=>{
                let color = colorThief.getColor(img);
                const rgbToHex = (r, g, b) => '#' + [r, g, b].map(x => {
                    const hex = x.toString(16)
                    return hex.length === 1 ? '0' + hex : hex
                }).join('')
                //hex颜色转rgb颜色
                function HexToRgb (str) {
                    var r = /^\#?[0-9A-F]{6}$/;
                    //replace替换查找的到的字符串
                    str = str.replace("#", "");
                    //match得到查询数组
                    var hxs = str.match(/../g);
                    for (var i = 0; i < 3; i++) hxs[i] = parseInt(hxs[i], 16);
                    return hxs;
                }
                function getDarkColor(color, level) {
                    var rgbc = HexToRgb(color);
                    //floor 向下取整
                    for (var i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level));
                    return rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
                }
                var hexcolor=rgbToHex(color[0], color[1], color[2]);
                var hexcolor1=getDarkColor(hexcolor,0.12)
                $('.maincolor').css('background-color',hexcolor1)
                $("input[name='maincolor']").val(hexcolor1);
                console.log(color)
            };
            if (img.complete) {
                getColorFun();
            } else {
                img.addEventListener('load', function () {
                    getColorFun();
                });
            }
        }
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: "{:url('admin/Upload/uploadLocal')}" //改成您自己的上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    //console.log(result)
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res)
                //如果上传失败
                if(res.code == 0){
                    $("input[name='thumb']").val(res.data);
                    $('#demo1').attr('src', res.data);
                    getcolor()

                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        /* 渲染laydate */
        laydate.render({
            elem: '#formBasDateSel',
            trigger: 'click',
            range: true
        });

        /* 监听表单提交 */
        form.on('submit(formBasSubmit)', function (data) {
            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            $.ajax({
                url:"{:url('ads_add')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    //console.log(res)
                    if (res.code == 0) {
                        layer.msg(res.msg,{icon:1,time:1500,shade:0.1})
                        setTimeout(function () {
                            admin.closeThisTabs();
                        }, 1000);
                    } else {
                        $(".layui-btn").removeClass('layui-disabled').removeAttr('disabled');
                        wk.error(res.msg);
                        return false;
                    }
                }
            })
        });
    });
</script>
</body>
</html>